/**
 * @Title: tableExpanderRow.js
 * @Description: TODO
 * Copyright: Copyright (c) 2014 
 * Company:中国平安
 *
 * @Project HIMS-FWA
 * @author ex-xieqi001
 * @date 2014年11月18日 下午11:31:44
 * @version V1.0
 */

;
(function(){
	
	var app = angular.module('fwa.directives');
	app.directive('tableExpanderRow',['$parse','$compile',function($parse,$compile){
		
		var contentTemp = 
			'<tr ng-show="contentShow">'+
			'</tr>';
		
		return {
			restrict: 'EA',
			scope:{
				change:'&',
				template:'=',
				contentShow:'=contentShow'
			},
			replace:true,
			template:
				'<a ng-click="onClick()">'+
					'<span ng-show="!spanName" class="glyphicon" ng-class="{true:\'glyphicon-minus\',false:\'glyphicon-plus\'}[contentShow]"></span>'+
					'<span ng-show="spanName" ng-bind="spanName"></span>'+
				'</a>',
			link:function($scope, $element, $attrs, $controller){
				var ngShow = $attrs.ngShow;
				var hasContent = $parse(ngShow)($scope.$parent);
				var contentElem;
				
				$scope.contentShow = $scope.contentShow || false;
				
				$scope.span = $scope.span || 1;
				
				$scope.spanName = $attrs.spanName;
				
				if(hasContent) {
					contentElem = $compile(contentTemp)($scope);
					$element.parent().parent().parent().after(contentElem);
					
					if($scope.template) {
						contentElem.append($compile($scope.template)($scope.$parent));
					}
				}
				
				$scope.onClick = function(){
					$scope.contentShow = !$scope.contentShow;
					
					$scope.change();
				}
				
			}
		};
		
	}]);
	
})();